<?php
namespace app\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Log;

class ShopMaster extends Model
{

    // 设置表名NAME="shop_master";
    // 设置表名
    const TABLE_NAME = "shop_master";

    protected $table = 'shop_master';

    protected $primaryKey = 'shop_id';

    protected $shopMaster = self::TABLE_NAME;

    public $timestamps = false;

    public function readShopMaster() // 查
    {
        return $this->all();
    }

    public function oneShopMaster($data, $arr) // 单条查询
    {
        return $this->where($data, $arr)
            ->get()
            ->toArray();
    }

    public function delShopMaster($shop_id) // 删
    {
        $sql = 'update shop_master set delete_flg = 1 where shop_id = ?';
        $param = [
            $shop_id
        ];
        $delflag = DB::update($sql, $param);
        return $delflag;
    }

    public function updShopMaster($data, $list, $arr) // 改
    {
        $ShopMaster = $this->where($list, '=', $data);
        return $ShopMaster->update($arr);
    }

    public function saveShopMaster($data) // 增
    {
        return DB::table('shop_master')->insert($data);
    }

    public function getShopInfoByLoginId($shopId)
    {
        $sql = 'select * from shop_master where shop_id = ? and delete_flg = 0';
        $param = [
            $shopId
        ];

        $shopInfo = DB::select($sql, $param);
        return $shopInfo;
    }

    public function getShopInfoByCorpId($corpId)
    {
        $sql = 'select * from corp_master where corp_id = ? and delete_flg = 0';
        $param = [
            $corpId
        ];

        $shopInfoCorp = DB::select($sql, $param);
        return $shopInfoCorp;
    }

    public function getShopMaster()
    {
        $sql = 'select * from shop_master where delete_flg = 0';

        $shopInfo = DB::select($sql);

        return $shopInfo;
    }

    public function addCsvShopMasterr($data)
    {
        if (isset($data[0]) && ! empty($data[0])) {

            $data[1] = intval($data[1]);
            $sql = 'INSERT INTO shop_master(shop_name, corp_id, shop_kana, shop_postcode, shop_adress, shop_biru_name, shop_telephone_number, shop_fax_number, stuff_name, stuff_kana, shop_mail, shop_tel, delete_flg, created_at, updated_at)
                VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';

            DB::insert($sql, $data);
        }
    }
}

